package cz.cvut.manatee.server;

import java.util.Date;

import com.google.gwt.user.server.rpc.RemoteServiceServlet;

import cz.cvut.manatee.client.RemoteServiceFrontend;
import cz.cvut.manatee.shared.AttributItem;
import cz.cvut.manatee.shared.CorpusInformations;
import cz.cvut.manatee.shared.concordance.CollectedSearchParameters;
import cz.cvut.manatee.shared.concordance.ResultTableRow;
import cz.cvut.manatee.shared.concordance.ResultTableWord;
import cz.cvut.manatee.shared.login.User;
import cz.cvut.manatee.shared.login.UserInfo;
import cz.cvut.manatee.shared.login.UserNotLogged;

public class RemoteServiceFrontendImpl extends RemoteServiceServlet implements RemoteServiceFrontend {

	private static final long serialVersionUID = 1L;

	public ResultTableRow[] fetchConcordanceResultsArray( CollectedSearchParameters search ){
	
		ResultTableRow[] linesArray = new ResultTableRow[2];

		Date date = new Date();
        int rand = (int)date.getTime() %100;
        
		String [] slova = {"Na","každém","poschodí","naproti","výtahovým","dveřím","na","něho","se","zdi","zírala","obrovská","tvář","z","plakátů",".","Byl","to","jeden","z","těch","obrazů",",","které","jsou","udělány","tak","důmyslně",",","že","vás","oči","sledují",",","kam","se","hnete",".","\"","Velký","bratr","tě","sleduje","\"",",","zněl","nápis","pod","obrazem",".","V","bytě","jakýsi","zvučný","hlas","předčítal","řadu","čísel",",","která","měla","cosi","společného","s","výrobou","šedé","litiny",".","Hlas","vycházel","z","obdélníkové","desky",",","jakéhosi","matného","zrcadla",",","jež","bylo","součástí"};
		int line, word;

		for(line=0;line<linesArray.length;line++){

			String [] attributArray = new String[3];
			ResultTableWord [] leftsArray = new ResultTableWord[3];
			ResultTableWord [] kwicssArray = new ResultTableWord[3];
			ResultTableWord [] rightsArray = new ResultTableWord[3];

			for(word=0;word<attributArray.length;word++){
				attributArray[word] = new String(slova[(rand++)%slova.length]);
			}

			for(word=0;word<leftsArray.length;word++){
				leftsArray[word] = new ResultTableWord(slova[(rand++)%(slova.length)], "lemma", "TAG");
				//leftsArray[word] = new ResultTableWord("sov", "lemma", "TAG");
			}

			for(word=0;word<kwicssArray.length;word++){
				kwicssArray[word] = new ResultTableWord(slova[(rand++)%(slova.length)], "lemma", "TAG");
				//kwicssArray[word] = new ResultTableWord("sov", "lemma", "TAG");
			}

			for(word=0;word<rightsArray.length;word++){
				rightsArray[word] = new ResultTableWord(slova[(rand++)%(slova.length)], "lemma", "TAG");
				//rightsArray[word] = new ResultTableWord("sov", "lemma", "TAG");
			}
			
			linesArray[line] = new ResultTableRow(attributArray, leftsArray, kwicssArray, rightsArray);
		}
		
		return linesArray;
	}

	public CorpusInformations[] fetchCorpusesAndAttributes( ){
		
		CorpusInformations[] corpusArray = new CorpusInformations[3];
		String[] nazvy = {"capek_uplny", "pmk", "orwel"};
		String[] nazvyLidske = {"Čapek úplný", "Pražský mluvený korpus", "Korpus s texty z \"1948\""};
		String[] info = {"Úplný soubor všech děl Karla Čapka který je něco něco něco", "Pražský mluvený korpus", "ručně označkovaný korpus Orwellova románu \"1984\""}; 
		
		
		for(int corpus=0; corpus<corpusArray.length; corpus++){
			
			if(corpus%2 == 0){
				AttributItem[] attributArray = new AttributItem[3];
				attributArray[0] = new AttributItem("lemma", "Lemma");
				attributArray[1] = new AttributItem("word", "Word");
				attributArray[2] = new AttributItem("tag", "Tag");

				corpusArray[corpus] = new CorpusInformations(nazvy[corpus],nazvyLidske[corpus],attributArray, info[corpus]); 
			}
			else{
				AttributItem[] attributArray = new AttributItem[1];
				attributArray[0] = new AttributItem("word", "Word");

				corpusArray[corpus] = new CorpusInformations(nazvy[corpus],nazvyLidske[corpus],attributArray, info[corpus]); 
			}
		}
		
		return corpusArray;
		
	}
	

	/*
	 * testovaci
	 */

	public CollectedSearchParameters collectedParameterMirror( CollectedSearchParameters search ){
		return search;
	}
	
	public ResultTableRow fetchConcordanceResults( CollectedSearchParameters search ){
		return new ResultTableRow();
	}
	
	/*
	 * testovaci
	 */
	public String fetchConcordanceResults( ){
		return "jede jede buzna kazdej ji tu pozna";
	}
	
	public User login(String login, String password) {
		if (login.equals("manatee") && password.equals("manatee")) {
			return new UserInfo(login, "Uživatel Manatee");
		} else {
			// return new UserNotLogged("Spatny login nebo heslo.");
			return new UserNotLogged("Špatný login beho heslo");
		}
	}
}
